package com.hongyun.tms.web.controller;

import com.hongyun.tms.common.PageResponseDto;
import com.hongyun.tms.common.annotation.excel.ExcelExport;
import com.hongyun.tms.common.dto.dislog.DisLogExportDto;
import com.hongyun.tms.common.dto.dislog.DisLogImportDto;
import com.hongyun.tms.common.dto.dislog.DisLogTemplateDto;
import com.hongyun.tms.common.dto.proddislog.TmsProdDisLogQueryDto;
import com.hongyun.tms.common.utils.MapperingUtils;
import com.hongyun.tms.common.vo.TmsProdDisLogVO;
import com.hongyun.tms.domain.AjaxResult;
import com.hongyun.tms.entity.TmsProdDisLog;
import com.hongyun.tms.infra.excel.EasyExcelUtil;
import com.hongyun.tms.infra.excel.listener.DisLogListener;
import com.hongyun.tms.service.TmsProdDisLogService;
import com.hongyun.tms.web.utils.FileUploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
* 调度日志
*
* @author yuheng
* @since 1.0.0 2024-06-12
*/
@RestController
@RequestMapping("plan/disLog")
@Api(value = "调度日志", tags = "调度日志")
@AllArgsConstructor
public class TmsProdDisLogController {
    private final TmsProdDisLogService tmsProdDisLogService;

    /**
    * 分页
    */
    @GetMapping("page")
    @ApiOperation(value = "分页", notes = "分页")
    public AjaxResult<PageResponseDto<TmsProdDisLogVO>> page(@Valid TmsProdDisLogQueryDto query){
        PageResponseDto<TmsProdDisLogVO> page = tmsProdDisLogService.page(query);
        return AjaxResult.success(page);
    }

    /**
    * 详情
    */
    @GetMapping("{id}")
    @ApiOperation(value = "详情", notes = "id")
    public AjaxResult<TmsProdDisLogVO> get(@PathVariable("id") Long id){
        TmsProdDisLog entity = tmsProdDisLogService.getById(id);
        return AjaxResult.success(MapperingUtils.copyProperties(entity, TmsProdDisLogVO.class));
    }

    /**
    * 新增
    */
    @PostMapping("/save")
    @ApiOperation(value = "新增", notes = "传入TmsProdDisLog")
    public AjaxResult save(@RequestBody TmsProdDisLogVO vo){
         tmsProdDisLogService.save(vo);
         return AjaxResult.success();
    }

    /**
    * 修改
    */
    @PostMapping("/update")
    public AjaxResult update(@RequestBody @Valid TmsProdDisLogVO vo){
        tmsProdDisLogService.update(vo);
        return AjaxResult.success();
    }

    /**
    * 删除
    */
    @PostMapping("/remove")
    @ApiOperation(value = "逻辑删除", notes = "传入ids")
    public AjaxResult delete(@ApiParam(value = "主键集合", required = true) @RequestParam(name = "ids") String ids){
         List<String> idList = Arrays.asList(ids.split(","));
        tmsProdDisLogService.removeByIds(idList);
        return AjaxResult.success();
    }

    @GetMapping("exportTemplate")
    @ExcelExport(value = DisLogTemplateDto.class)
    public AjaxResult exportTemplate(HttpServletResponse response) {
        return AjaxResult.success(Collections.emptyList());
    }

    @GetMapping("exportData")
    @ExcelExport(value = DisLogExportDto.class)
    public AjaxResult<List<DisLogExportDto>> exportData(TmsProdDisLogQueryDto query, HttpServletResponse response) {
        List<DisLogExportDto> exportList = tmsProdDisLogService.export(query);
        return AjaxResult.success(exportList);
    }

    @ApiOperation("导入调度日志")
    @PostMapping("/importLog")
    public AjaxResult<Void> importLog(MultipartFile file) throws IOException {
        AjaxResult<Void> validationResult = FileUploadUtil.validateExcelFile(file);
        // 根据code判断校验是否通过
        if (validationResult.getCode() != HttpStatus.OK.value()) {
            return validationResult;
        }

        // 创建 Excel 数据处理监听器
        DisLogListener listener = new DisLogListener(tmsProdDisLogService);

        // 处理 Excel 文件
        try (InputStream inputStream = file.getInputStream()) {
            EasyExcelUtil.syncReadModel(inputStream, listener, DisLogImportDto.class, 0, 1);
            return AjaxResult.success();
        }
    }
}
